﻿using BySoft.Server.Business.UserCode;
using BySoft.Server.Common;
using BySoft.Server.Consts;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.Plan
{
    public partial class PlanView : AuthBasePage
    {
        public DataTable PlanDetailDT
        {
            set { Session["PlanDetailDT"] = value; }
            get
            {
                if (Session["PlanDetailDT"] == null)
                {
                    return CreateDT();
                }
                else
                {
                    return (DataTable)Session["PlanDetailDT"];
                }
            }
        }
        public DataTable CreateDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("PlanDetailID");
            dt.Columns.Add("BOMDetailID");
            dt.Columns.Add("IsDirect");
            dt.Columns.Add("MaterialID");
            dt.Columns.Add("MaterialName");
            dt.Columns.Add("Long");
            dt.Columns.Add("Width");
            dt.Columns.Add("Height");
            dt.Columns.Add("OutSide");
            dt.Columns.Add("InSide");
            dt.Columns.Add("Specifications");
            dt.Columns.Add("Unit");
            dt.Columns.Add("MeterWeight");
            dt.Columns.Add("MMNumber");
            dt.Columns.Add("TotalNum");
            dt.Columns.Add("TotalWeight");
            dt.Columns.Add("IsNew");
            dt.Columns.Add("IsDel");
            return dt;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PlanDetailDT = null;
                InitData();
            }
            txtWorkOrderNo.Attributes.Add("readonly", "true");
            txtCustomerPartNo.Attributes.Add("readonly", "true");
            txtProductID.Attributes.Add("readonly", "true");
            txtProductName.Attributes.Add("readonly", "true");
            txtSpecifications.Attributes.Add("readonly", "true");
            txtUnit.Attributes.Add("readonly", "true");
            txtInSide.Attributes.Add("readonly", "true");
            txtOutSide.Attributes.Add("readonly", "true");
            txtOrderNum.Attributes.Add("readonly", "true");
            txtLastNum.Attributes.Add("readonly", "true");
            Master.ValidationGroup = "gSubmit";
        }

        private void InitData()
        {
            string id = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("id"));
            if (!string.IsNullOrEmpty(id))
            {
                DataTable dt = BsPlanBusiness.GetDataByPlanID(id);
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow dr = dt.Rows[0];
                    lblPlanID.Text = CommonMethod.GetString(dr["PlanID"]);
                    lblBillDate.Text = string.Format("{0:yyyy-MM-dd}", dr["BillDate"]);
                    hdBOMID.Value = CommonMethod.GetString(dr["BOMID"]);
                    txtWorkOrderNo.Text = CommonMethod.GetString(dr["WorkOrderNo"]);
                    txtCustomerPartNo.Text = CommonMethod.GetString(dr["CustomerPartNo"]);
                    txtProductID.Text = CommonMethod.GetString(dr["ProductID"]);
                    txtProductName.Text = CommonMethod.GetString(dr["ProductName"]);
                    txtSpecifications.Text = CommonMethod.GetString(dr["ProductSpecifications"]);
                    txtUnit.Text = CommonMethod.GetString(dr["ProductUnit"]);
                    txtInSide.Text = CommonMethod.GetString(dr["ProductInSide"]);
                    txtOutSide.Text = CommonMethod.GetString(dr["ProductOutSide"]);
                    txtOrderNum.Text = CommonMethod.GetString(dr["Nums"]);
                    txtLossNum.Text = CommonMethod.GetString(dr["LossNum"]);
                    txtLastNum.Text = CommonMethod.GetString(dr["LastNum"]);
                    txtPlanNum.Text = CommonMethod.GetString(dr["PlanNum"]);
                    lblProcess.Text = CommonMethod.GetString(dr["ProcessAttribute"]);
                    txtUseStockNum.Text = CommonMethod.GetString(dr["UseStockNum"]);
                    txtProduceNum.Text = CommonMethod.GetString(dr["ProduceNum"]);
                    txtRemark.Text = CommonMethod.GetString(dr["Remark"]);
                    DataTable dtemp = PlanDetailDT.Copy();
                    foreach (DataRow d in dt.Rows)
                    {
                        DataRow row = dtemp.NewRow();
                        row["PlanDetailID"] = CommonMethod.GetString(d["PlanDetailID"]);
                        row["BOMDetailID"] = CommonMethod.GetString(d["BOMDetailID"]);
                        row["IsDirect"] = CommonMethod.GetString(d["IsDirect"]);
                        row["MaterialID"] = CommonMethod.GetString(d["MaterialID"]);
                        row["MaterialName"] = CommonMethod.GetString(d["MaterialName"]);
                        row["Long"] = CommonMethod.GetString(d["Long"]);
                        row["Width"] = CommonMethod.GetString(d["Width"]);
                        row["Height"] = CommonMethod.GetString(d["Height"]);
                        row["OutSide"] = CommonMethod.GetString(d["OutSide"]);
                        row["InSide"] = CommonMethod.GetString(d["InSide"]);
                        row["Specifications"] = CommonMethod.GetString(d["Specifications"]);
                        row["Unit"] = CommonMethod.GetString(d["Unit"]);
                        row["MeterWeight"] = CommonMethod.GetString(d["MeterWeight"]);
                        row["MMNumber"] = CommonMethod.GetString(d["MMNumber"]);
                        row["TotalNum"] = CommonMethod.GetString(d["TotalNum"]);
                        row["TotalWeight"] = CommonMethod.GetString(d["TotalWeight"]);
                        row["IsNew"] = "-1";
                        row["IsDel"] = "-1";
                        dtemp.Rows.Add(row);
                    }
                    PlanDetailDT = dtemp.Copy();
                }
            }
            BindGridView();
        }

        private void BindGridView()
        {
            DataView dv = new DataView(PlanDetailDT);
            dv.RowFilter = " IsDel <> '1'";
            //dv.Sort = "BOMDetailID ";
            DataTable dt = dv.ToTable();
            if (dt != null && dt.Rows.Count > 0)
            {
                grdResult.DataSource = dt;
                grdResult.DataBind();
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.UpdatePanel4, "iframeAutoFit();");
        }
    }
}